﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using ncp.Business;
using ncp.Business.Common;

namespace ncp.Web.cms
{
    public partial class News_Index : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            var userInfo = WebMasterCookie.GetCookie(CommonConst.adminCookieName);
            if (userInfo==null)
            {
                Response.Write(" <script> top.location.href='/cms/login.aspx'; </script> ");
            }
            AjaxPro.Utility.RegisterTypeForAjax(typeof(News_Index));
        }

        //装载商品类别表视图
        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public string ShowNewsClassTree()
        {
            var jsonString = "";//返回的json字符串
            Dictionary<string, object> jsonResult = new Dictionary<string, object>();
            try
            {
                DataTable dt = NewsInfoBll.GetNewsType("1",  CommonConst.DB_Name);
                jsonString = "<ul id=\"tt1\" class=\"easyui-tree\" animate=\"true\" dnd=\"false\">";
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    jsonString += "<li id=\"" + dt.Rows[i]["NC_Code"].ToString() + "\">";
                    jsonString += "<span>" + dt.Rows[i]["NC_Name"].ToString() + "</span>";
                    jsonString += "</li>";
                }
                jsonString += "</ul>";
                jsonResult.Add("msg", jsonString);
                jsonResult.Add("isreload", true);
            }
            catch (Exception ex)
            {
                jsonResult.Clear();
                jsonResult.Add("msg", ex.Message);
                jsonResult.Add("isreload", false);
            }
            return "[" + JsonHelper.ObjectToJSON(jsonResult) + "]";
        }

        //得到总记录数
        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public int GetRecordCount(string currentwhere)
        {
            return NewsInfoBll.GetNewsInfoCount(currentwhere, CommonConst.DB_Name);
        }

        //得到初始条件
        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public string GetstrWhere(string pQueryName, string pQueryOperator, string pQueryValue)
        {
            string myWhere = "1=1";
            if (pQueryValue != "")
            {
                if (pQueryOperator == "like")
                {
                    myWhere += " and " + pQueryName + " like '%" + pQueryValue + "%'";
                }
                else
                {
                    myWhere += " and " + pQueryName + " " + pQueryOperator + " '" + pQueryValue + "'";
                }
            }
            return myWhere;
        }

        //装载商品表视图
        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public string BindTable(string jsonData)
        {
            var dictList = JsonHelper.JSONToObject<Dictionary<string, object>>(jsonData);
            DataTable dt = NewsInfoBll.GetNewsInfoTable(int.Parse(dictList["currentpageindex"].ToString()) - 1, int.Parse(dictList["pagesize"].ToString()), "*", dictList["sorting"].ToString() + " " + dictList["sortdir"].ToString(), dictList["currentwhere"].ToString(), CommonConst.DB_Name);
            return JsonHelper.DataTableToJSON(dt);
        }

        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public string OnSave(string json)
        {
            Dictionary<string, object> jsonResult = new Dictionary<string, object>();
            var userInfo = WebMasterCookie.GetCookie(CommonConst.adminCookieName);
            if (userInfo != null)
            {
                try
                {
                    var dicProj = JsonHelper.JSONToObject<Dictionary<string, object>>(json);
                    var description = Server.HtmlEncode(dicProj["description"].ToString()).Replace("/", "");
                    dicProj.Remove("description");
                    dicProj.Add("description", description);
                    dicProj.Add("user_id", userInfo.Id);
                    if (dicProj["newsId"].ToString() == "")
                    {
                        //添加
                        int count = NewsInfoBll.InsertNewsInfo(dicProj, CommonConst.DB_Name);
                        if (count > 0)
                        {
                            jsonResult.Add("isreload", true);
                            jsonResult.Add("msg", "信息添加成功!");
                        }
                        else
                        {
                            jsonResult.Add("isreload", false);
                            jsonResult.Add("msg", "信息添加失败!");
                        }
                    }
                    else
                    {
                        //修改
                        NewsInfoBll.UpdateNewsInfo(dicProj, CommonConst.DB_Name);
                        var dicHandleLog = new Dictionary<string, object>();
                        dicHandleLog.Add("user_id", userInfo.Id);
                        dicHandleLog.Add("computername", userInfo.Computername);
                        dicHandleLog.Add("computerip", userInfo.Computerip);
                        dicHandleLog.Add("handle_note", "修改系统新闻");
                        dicHandleLog.Add("obj_guid", "News_Id=" + dicProj["newsId"]);
                        LogHandleBll.AddHandleLog(dicHandleLog, CommonConst.DB_Name);
                        jsonResult.Add("isreload", true);
                        jsonResult.Add("msg", "信息修改成功!");
                    }
                }
                catch (Exception ex)
                {
                    jsonResult.Clear();
                    jsonResult.Add("isreload", false);
                    jsonResult.Add("msg", ex.Message);
                }
            }
            else
            {
                jsonResult.Add("isreload", false);
                jsonResult.Add("msg", "登录超时，请退出重新登录！");
            }

            return "[" + JsonHelper.ObjectToJSON(jsonResult) + "]";
        }

        //删除商品信息
        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public string DeleteNewsInfo(int newsId)
        {
            Dictionary<string, object> jsonResult = new Dictionary<string, object>();
            try
            {
                int ii = NewsInfoBll.DeleteNewsInfo(newsId.ToString(), CommonConst.DB_Name);
                if (ii == 0)
                {
                    jsonResult.Add("msg", "该文章不存在或者已经被删除！");
                    jsonResult.Add("isreload", false);
                }
                else
                {
                    var userInfo = WebMasterCookie.GetCookie(CommonConst.adminCookieName);
                    Dictionary<string, object> dicHandleLog = new Dictionary<string, object>();
                    dicHandleLog.Add("user_id", userInfo.Id);
                    dicHandleLog.Add("computername", userInfo.Computername);
                    dicHandleLog.Add("computerip", userInfo.Computerip);
                    dicHandleLog.Add("handle_note", "删除系统新闻");
                    dicHandleLog.Add("obj_guid", "News_Id=" + newsId.ToString());
                    LogHandleBll.AddHandleLog(dicHandleLog, CommonConst.DB_Name);
                    jsonResult.Add("msg", "删除成功！");
                    jsonResult.Add("isreload", true);
                }
            }
            catch (Exception ex)
            {
                jsonResult.Clear();
                jsonResult.Add("msg", ex.Message);
                jsonResult.Add("isreload", false);
            }
            return "[" + JsonHelper.ObjectToJSON(jsonResult) + "]";
        }

        //修改文章状态
        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public string SetNewsStatus(int newsId,int status)
        {
            Dictionary<string, object> jsonResult = new Dictionary<string, object>();
            Dictionary<string, object> dicNews = new Dictionary<string, object>();
            Dictionary<string, object> dicWhere = new Dictionary<string, object>();
            dicWhere.Add("News_Id", newsId);
            dicNews.Add("News_Status", status);
            try
            {
                NewsInfoBll.UpdateNewsInfo(dicNews, dicWhere, CommonConst.DB_Name);

                var userInfo = WebMasterCookie.GetCookie(CommonConst.adminCookieName);
                Dictionary<string, object> dicHandleLog = new Dictionary<string, object>();
                dicHandleLog.Add("user_id", userInfo.Id);
                dicHandleLog.Add("computername", userInfo.Computername);
                dicHandleLog.Add("computerip", userInfo.Computerip);
                dicHandleLog.Add("handle_note", ((status == 0) ? "禁用系统新闻" : "启用系统新闻"));
                dicHandleLog.Add("obj_guid", "News_Id=" + newsId.ToString());
                LogHandleBll.AddHandleLog(dicHandleLog, CommonConst.DB_Name);

                jsonResult.Add("isreload", true);
                jsonResult.Add("msg", "信息修改成功!");
            }
            catch (Exception ex)
            {
                jsonResult.Clear();
                jsonResult.Add("msg", ex.Message);
                jsonResult.Add("isreload", false);
            }
            return "[" + JsonHelper.ObjectToJSON(jsonResult) + "]";
        }

        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public string SetNewsFlag(string newsId, string status)
        {
            Dictionary<string, object> jsonResult = new Dictionary<string, object>();
            Dictionary<string, object> dicNews = new Dictionary<string, object>();
            Dictionary<string, object> dicWhere = new Dictionary<string, object>();
            dicNews.Add("News_Flag", newsId);
            dicWhere.Add("News_Status", status);
            try
            {
                NewsInfoBll.UpdateNewsInfo(dicNews, dicWhere, CommonConst.DB_Name);
                jsonResult.Add("isreload", true);
                jsonResult.Add("msg", "信息修改成功!");
            }
            catch (Exception ex)
            {
                jsonResult.Clear();
                jsonResult.Add("msg", ex.Message);
                jsonResult.Add("isreload", false);
            }
            return "[" + JsonHelper.ObjectToJSON(jsonResult) + "]";
        }
    }
}
